package com.yunhe.merchants.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.merchants.model.CustomerStatistics;
import com.yunhe.merchants.model.PersonalPlanComplete;
import com.yunhe.merchants.model.SalesFunnel;
import com.yunhe.merchants.service.IStatisticsAnalysisService;
import com.yunhe.merchants.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName: StatisticalAnalysisController
 * @Description:
 * @author: GongDaCheng
 * @Date: 10:07 2021/5/17
 */
@Api(value = "statisticalAnalysis", description = "统计分析", tags = {"Model_STATISTICAL_ANALYSIS"})
@RestController
@RequestMapping("/statisticalAnalysis")
public class StatisticalAnalysisController {

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    IStatisticsAnalysisService statisticsAnalysisService;

    @ApiOperation("客户数据统计")
    @GetMapping(value = "/customerStatistics")
    public ResponseEntity<ResultObject> getCustomerStatistics(@RequestParam(value = "year") String year,
                                                              @RequestParam(value = "month") String month) throws ArgumentErrorException {
        Long userId = myHeaderUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        CustomerStatistics customerStatistics = statisticsAnalysisService.getCustomerStatistics(userId,year, month);
        return new ResponseEntity<>(new ResultObject<>(customerStatistics), HttpStatus.OK);
    }

    @ApiOperation("销售漏斗图")
    @GetMapping(value = "/salesFunnel")
    public ResponseEntity<ResultObject> getSalesFunnel() throws ArgumentErrorException {
        Long userId = myHeaderUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        SalesFunnel salesFunnel = statisticsAnalysisService.getSalesFunnel(userId);
        return new ResponseEntity<>(new ResultObject<>(salesFunnel), HttpStatus.OK);
    }

    @ApiOperation("渠道来源分析")
    @GetMapping(value = "/channelSource")
    public ResponseEntity<ResultObject> getChannelSource() throws ArgumentErrorException {
        Long userId = myHeaderUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        return new ResponseEntity<>(new ResultObject<>(statisticsAnalysisService.getChannelSource(userId)), HttpStatus.OK);
    }

    @ApiOperation("人员计划完成情况")
    @GetMapping(value = "/planCompletion")
    public ResponseEntity<ResultObject> planCompletion(@RequestParam(value = "stationId") Long stationId,
                                                       @RequestParam(value = "year") String year,
                                                       @RequestParam(value = "month") String month) throws ArgumentErrorException {
        Long userId = myHeaderUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        List<PersonalPlanComplete> personalPlanCompletes = statisticsAnalysisService.planCompletion(stationId,userId,year, month);
        return new ResponseEntity<>(new ResultObject<>(personalPlanCompletes), HttpStatus.OK);
    }
}
